package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsLibraryArea;
import com.jy.wms.pojo.WmsLibraryPosition;
import com.jy.wms.pojo.WmsWarehouse;
import com.jy.wms.service.WmsLibraryPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsLibraryPositionController
 * @Description: 库位表
 * @Author yjs
 * @Date Tue Aug 29 15:58:05 GMT+08:00 2017
 */
@Controller
@RequestMapping("/wmslibraryposition")
@Api(value = "/wmslibraryposition", description = "关于库位信息操作")
public class  WmsLibraryPositionController extends BaseController<WmsLibraryPositionService,WmsLibraryPosition> {

    @ApiOperation(value = "库位管理 POST请求查询库位信息",response = JSONObject.class, notes = "POST请求查询库位信息")
    @PostMapping(value="/list")
    public @ResponseBody JSONObject getWmsLibraryPosition(@RequestBody WmsLibraryPosition entity,HttpSession session) {
        JSONObject json = getJSONObject();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckmc=String.valueOf(userinfo.getUser().getOrgName());//仓库名称
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
        entity.setCkid(ckid);
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
        if (hz.size() > 0) {
            entity.setSshzid(hz.get(0).getHzid());
        }
        try {
            entity.setCkid(ckid);
            entity.setCkmc(ckmc);
            JSONObject jsonObject= baseService.baseSelectPage(entity);
            List<WmsLibraryPosition> list= (List<WmsLibraryPosition>) jsonObject.get("list");
            /*for(int i=0;i<list.size();i++){
                list.get(i).setCkmc(ckmc);
                list.get(i).setCkid(ckid);
            }*/
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (Integer) jsonObject.get("count"));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "/delete")
    @ApiOperation(value = "库位信息 多个或单个库位信息",response = JSONObject.class, notes = "POST删除多个或单个库位信息")
    public @ResponseBody
    JSONObject deleteWmsLibraryPosition(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            baseService.baseDeleteBathPrimaryKeys(setModel.getIds(), hzid, ckid);
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "删除库位信息成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"删除库位信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"删除库位信息失败","删除库位信息失败","");
        }
        return json;
    }
    @PostMapping(value = "/add")
    @ApiOperation(value = "库位信息 新增库位信息",response = JSONObject.class, notes = "POST新增库位信息")
    public @ResponseBody JSONObject addWmsLibraryPosition(@RequestBody WmsLibraryPosition entity,HttpSession session) {
        JSONObject json = getJSONObject();

        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String ckmc=String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            baseService.searchADD(entity.getMc(),ckid);
            entity.setId(UUIDUtils.uuid());
            entity.setSshzid("0");//货主编号 默认是0
            entity.setSfyx("1");//是否有效 0无效 1有效(默认)
            entity.setZyzt("0");//占用状态 0：未占用(默认) 1：已占用
            entity.setCkid(ckid);
            baseService.baseInsert(entity);
            WmsLibraryArea wmsLibraryArea = baseService.searchKQ(entity.getSskqid(),ckid);
            entity.setCkmc(ckmc);
            entity.setKqmc(wmsLibraryArea.getMc());
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增库位成功","", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增库位失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增库位失败","新增库位信息失败","");
        }
        return json;
    }

    /**
     * 查询全部库区信息
     * @param map
     * @return
     */
    @PostMapping(value = "/addSearchArea")
    @ApiOperation(value = "库位信息 新增库位信息",response = JSONObject.class, notes = "查询输入{\"mc\":\"库区信息模糊查询\"}")
    public @ResponseBody JSONArray addSearch(@RequestBody Map<String,String> map,HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String id=map.get("id");
            if(id!=null && !"".equals(id)){
                WmsLibraryArea wmsLibraryArea=baseService.searchKQ(id,ckid);
                JSONObject json = getJSONObject();
                if(wmsLibraryArea!=null){
                    json.put("dictionaryid",wmsLibraryArea.getId());
                    json.put("value",wmsLibraryArea.getMc());
                    jsonArray.add(json);
                }else{
                    json.put("dictionaryid","");
                    json.put("value","");
                    jsonArray.add(json);
                }

            }else{
                List<WmsLibraryArea> list = baseService.addSearchArea(map.get("mc"),ckid);
                for (int i = 0; i < list.size(); i++) {
                    WmsLibraryArea wmsLibraryArea =  list.get(i);
                    JSONObject json = getJSONObject();
                    json.put("dictionaryid",wmsLibraryArea.getId());
                    json.put("value",wmsLibraryArea.getMc());
                    jsonArray.add(json);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }


    @GetMapping(value = "/getWmsLibraryPosition/{id}")
    @ApiOperation(value = "库位信息 查看单个库位信息",response = JSONObject.class, notes = "GET查看单个库位信息")
    public @ResponseBody JSONObject getWmsLibraryPositionById(@PathVariable("id") String id,HttpSession session) {
        JSONObject json = getJSONObject();
        WmsLibraryPosition entity= null;
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String ckmc=String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            entity = baseService.baseSelectByPrimaryKey(id,ckid);
            entity.setCkmc(ckmc);
            json = JSONObject.fromObject(entity);
        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看单个库位信息","查看库位信息失败","");
        }
        return json;
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "库位信息 修改库位信息",response = JSONObject.class, notes = "POST修改库位信息")
    public @ResponseBody JSONObject editWmsLibraryPosition(@RequestBody WmsLibraryPosition entity,HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String ckmc=String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            WmsLibraryPosition entitys=baseService.searchADDs(entity.getMc(),entity.getId(),ckid);
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if (hz.size() > 0) {
                entity.setSshzid(hz.get(0).getHzid());
            }
            if(entitys!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"编辑库位重复","编辑库位名称重复","");
            }else {
                entity.setCkid(ckid);
                baseService.baseUpdateByPrimaryKeySelective(entity);
                WmsLibraryArea wmsLibraryArea = baseService.searchKQ(entity.getSskqid(),ckid);
                entity.setCkmc(ckmc);
                entity.setKqmc(wmsLibraryArea.getMc());
            }
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"编辑库位成功","", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑库位失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑库位失败","编辑库位信息失败","");
        }
        return json;
    }

    /**
     * 请求查询对应类型的库位
     * @param map
     * @param session
     * @return
     */
    @ApiOperation(value = "库位管理 POST请求查询对应类型的库位",response = JSONObject.class, notes = "POST请求查询对应类型的库位")
    @PostMapping(value="/selectLibraryByLx")
    public @ResponseBody JSONObject getSelectLibraryByLx(@RequestBody Map<String,Object> map, HttpSession session) {
        List<WmsLibraryPosition> list = new Page<WmsLibraryPosition>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            if(map.containsKey("mc")) {
                list = baseService.baseSelectByLx(map.get("lx").toString(), map.get("mc").toString(),ckid);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询库位信息失败","查询对应类型的库位失败","");
        }
        return json;
    }
    @PostMapping(value = "getLibrarysBymc")
    @ApiOperation(value = "库位管理 查看库位id",response = JSONObject.class, notes = "查看库位ID")
    public @ResponseBody JSONObject getLibrarysBymc(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json =getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String owner=null;//取当前货主编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String mc="";
            if(map.containsKey("mc")){
                mc=map.get("mc").toString();
            }
            String lx="";
            if(map.containsKey("lx")){
                lx=map.get("lx").toString();
            }
            List<WmsLibraryPosition> list=baseService.selectBymc(owner,mc,ckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        } catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询库位失败","查询库位失败","");
        }
        return json;
    }
    @PostMapping(value = "getZCLibrarysBymc")
    @ApiOperation(value = "码盘收货 查看库位ID",response = JSONObject.class, notes = "查看库位ID")
    public @ResponseBody JSONObject getZCLibrarysBymc(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json =getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String owner=null;
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String mc="";
            if(map.containsKey("mc")){
                mc=map.get("mc").toString();
            }
            String lx="";
            if(map.containsKey("lx")){
                lx=map.get("lx").toString();
            }
            List<WmsLibraryPosition> list=baseService.searchZcqkws(mc,lx,owner,ckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        } catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询库位失败",e.getMessage(),"");
        }
        return json;
    }
}
